<template>
  <!-- 查询 -->
  <div class="container">
    <div class="main">
      <div class="header">
        <el-button type="primary" @click="add">新建</el-button>
        <el-input placeholder="请输入关键字搜索" v-model="searchTable" />
        <el-button type="info" @click="query">查询</el-button>
        <el-button type="info" @click="SX">重置</el-button>
      </div>
      <el-table :data="tableData" border class="eltable">
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column prop="wldb_code" label="调拨单号"></el-table-column>
        <el-table-column prop="wldb_dblx" label="调拨类型"></el-table-column>
        <el-table-column prop="wldb_jhdbsj" label="计划调拨时间"></el-table-column>
        <el-table-column prop="ck_name" label="转出仓库"></el-table-column>
        <el-table-column prop="rk_name" label="转入仓库"></el-table-column>
        <el-table-column prop="wldb_state" label="状态">
          <template #default="{ row }">
            <span v-if="row.wldb_state == 1">
              {{ zt.obj1 }}
            </span>
            <span v-if="row.wldb_state == 2">
              {{ zt.obj2 }}
            </span>
            <span v-if="row.wldb_state == 3">
              {{ zt.obj3 }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="wldb_bz" label="备注"></el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-space>
              <el-tooltip content="详情" placement="top">
                <div class="operation-item" @click="xq(row)">
                  <el-icon class="operation-icon">
                  </el-icon>
                  <span>详情</span>
                </div>
              </el-tooltip>
              <span v-if="row.wldb_state == 1">
                <el-tooltip content="编辑" placement="top">
                  <div class="operation-item" @click="editRow(row)">
                    <el-icon class="operation-icon">
                      <Edit />
                    </el-icon>
                    <span>编辑</span>
                  </div>
                </el-tooltip>
              </span>
              <span v-if="row.wldb_state == 1">
                <el-tooltip content="发起调拨" placement="top">
                  <div class="operation-item" @click="fqdb(row)">
                    <el-icon class="operation-icon delete">
                    </el-icon>
                    <span>发起调拨</span>
                  </div>
                </el-tooltip>
              </span>

            </el-space>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize"
        :total="totalItems" layout="total, prev, pager, next, jumper">
      </el-pagination>
    </div>
  </div>
  <!-- 添加 -->
  <el-dialog v-model="dialogVisible" title="添加" height="800">
    <el-form :model="form" :inline="true" class="demo-form-inline">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="调拨类型">
            <el-select v-model="form.wldb_dblx" placeholder="选择调拨类型" style="width: 200px;"
              @change="handleLeaderChange">
              <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划调拨时间">
            <el-date-picker v-model="form.wldb_jhdbsj" type="date" placeholder="Pick a day" :size="size" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="转出仓库">
            <el-select v-model="form.ckw_code" placeholder="选择转出仓库" style="width: 200px" @change="handleLeaderChange">
              <el-option v-for="item in cktableData" :key="item.ck_code" :label="item.ck_name" :value="item.ck_code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="转入仓库">
            <el-select v-model="form.rkw_code" placeholder="选择转入仓库" style="width: 200px" @change="handleLeaderChange">
              <el-option v-for="item in cktableData" :key="item.ck_code" :label="item.ck_name" :value="item.ck_code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注">
            <el-input v-model="form.wldb_bz"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-button type="primary" @click="addwl" style="float: right;">+添加物料</el-button>
    <el-table :data="selectedRows" border class="eltable">
      <el-table-column prop="kc_id" label="序号" width="50"></el-table-column>
      <el-table-column prop="wl_code" label="物料编码"></el-table-column>
      <el-table-column prop="wl_name" label="物料名称"></el-table-column>
      <el-table-column prop="wl_type" label="物料类型"></el-table-column>
      <el-table-column prop="wl_size" label="规格型号"></el-table-column>
      <el-table-column prop="kc_sl" label="库存数量"></el-table-column>
      <el-table-column label="可调拨数量">
        <template #default="{ row }">
          {{ row.kc_sl - row.kc_jhsl }}
        </template>
      </el-table-column>
      <el-table-column label="申请调拨数量">
        <template #default="{ row }">
          <el-input v-model="row.kc_sqsl" placeholder="请输入数量" type="number" min="0" :max="row.kc_sl - row.kc_jhsl"
            @input="handleInput(row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template #default="{ row }">
          <el-space>
            <el-tooltip content="删除" placement="top">
              <div class="operation-item" @click="deleteRow(row)">
                <el-icon class="operation-icon delete">
                  <Delete />
                </el-icon>
                <span>删除</span>
              </div>
            </el-tooltip>
          </el-space>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addqr">确 定</el-button>
    </span>
  </el-dialog>
  <!-- 库存 -->
  <el-dialog v-model="wldialogVisible" title="物料" width="800" :before-close="handleClose">
    <span>
      <el-table :data="wltableData" border class="eltable" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="kc_id" label="序号" width="50"></el-table-column>
        <!-- <el-table-column prop="ck_name" label="仓库名称" ></el-table-column>  
              <el-table-column prop="kq_name" label="库区名称" ></el-table-column>  
              <el-table-column prop="kw_name" label="库位名称" ></el-table-column>   -->
        <el-table-column prop="wl_code" label="物料编码"></el-table-column>
        <el-table-column prop="wl_name" label="物料名称"></el-table-column>
        <el-table-column prop="wl_type" label="物料类型"></el-table-column>
        <el-table-column prop="wl_size" label="规格型号"></el-table-column>
        <el-table-column prop="kc_sl" label="库存数量"></el-table-column>
        <el-table-column prop="kc_jhsl" label="计划数量"></el-table-column>
      </el-table>
      <el-button @click="wlqx">取 消</el-button>
      <el-button type="primary" @click="wlqr">确 定</el-button>
    </span>
  </el-dialog>
  <!-- 详情 -->
  <el-dialog v-model="xqdialogVisible" title="详情" height="800">
    <el-form :model="xqform" :inline="true" class="demo-form-inline">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="调拨类型">
            <el-input v-model="xqform.wldb_dblx"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划调拨时间">
            <el-input v-model="xqform.wldb_jhdbsj"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="转出仓库">
            <el-input v-model="xqform.ckw_code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="转入仓库">
            <el-input v-model="xqform.rkw_code"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注">
            <el-input v-model="xqform.wldb_bz"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-button type="primary" @click="addwl" style="float: right;">+添加物料</el-button>
    <el-table :data="xqtableData" border class="eltable">
      <el-table-column prop="wldbmx_id" label="序号" width="50"></el-table-column>
      <el-table-column prop="wl_code" label="物料编码"></el-table-column>
      <el-table-column prop="wldbmx_wlmc" label="物料名称"></el-table-column>
      <el-table-column prop="wldbmx_wllx" label="物料类型"></el-table-column>
      <el-table-column prop="wldbmx_ggxh" label="规格型号"></el-table-column>
      <el-table-column prop="kc_sl" label="库存数量"></el-table-column>
      <el-table-column label="可调拨数量">
        <template #default="{ row }">
          {{ row.kc_sl - row.kc_jhsl }}
        </template>
      </el-table-column>
      <el-table-column prop="wldbmx_sqdbsl" label="申请调拨数量"></el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="xqdialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
  <!-- 编辑 -->
  <el-dialog v-model="xgdialogVisible" title="编辑" height="800">
    <el-form :model="xgform" :inline="true" class="demo-form-inline">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="调拨类型">
            <el-input v-model="xgform.wldb_dblx"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划调拨时间">
            <el-date-picker v-model="xgform.wldb_jhdbsj" type="date" placeholder="Pick a day" :size="size" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="转出仓库">
            <el-select v-model="xgform.ckw_code" placeholder="选择转出仓库" style="width: 200px" @change="handleLeaderChange">
              <el-option v-for="item in cktableData" :key="item.ck_code" :label="item.ck_name" :value="item.ck_code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="转入仓库">
            <el-select v-model="xgform.rkw_code" placeholder="选择转入仓库" style="width: 200px" @change="handleLeaderChange">
              <el-option v-for="item in cktableData" :key="item.ck_code" :label="item.ck_name" :value="item.ck_code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注">
            <el-input v-model="xgform.wldb_bz"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-button type="primary" @click="xgwl" style="float: right;">+添加物料</el-button>
    <el-table :data="xqtableData" border class="eltable">
      <el-table-column prop="wldbmx_id" label="序号" width="50"></el-table-column>
      <el-table-column prop="wl_code" label="物料编码"></el-table-column>
      <el-table-column prop="wldbmx_wlmc" label="物料名称"></el-table-column>
      <el-table-column prop="wldbmx_wllx" label="物料类型"></el-table-column>
      <el-table-column prop="wldbmx_ggxh" label="规格型号"></el-table-column>
      <el-table-column prop="kc_sl" label="库存数量"></el-table-column>
      <el-table-column label="可调拨数量">
        <template #default="{ row }">
          {{ row.kc_sl - row.kc_jhsl }}
        </template>
      </el-table-column>
      <el-table-column prop="wldbmx_sqdbsl" label="申请调拨数量">
        <template #default="{ row }">
          <el-input v-model="row.wldbmx_sqdbsl" placeholder="请输入数量" type="number" min="0" :max="row.kc_sl - row.kc_jhsl"
            @input="handleInput1(row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template #default="{ row }">
          <el-space>
            <el-tooltip content="删除" placement="top">
              <div class="operation-item" @click="deleteRow1(row)">
                <el-icon class="operation-icon delete">
                  <Delete />
                </el-icon>
                <span>删除</span>
              </div>
            </el-tooltip>
          </el-space>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="xgdialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="xgqr">确 定</el-button>
    </span>
  </el-dialog>
  <!-- 库存 -->
  <el-dialog v-model="xgwldialogVisible" title="物料" width="800" :before-close="handleClose">
    <span>
      <el-table :data="wlxgtableData" border class="eltable" @selection-change="xghandleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="kc_id" label="序号" width="50"></el-table-column>
        <el-table-column prop="wl_code" label="物料编码"></el-table-column>
        <el-table-column prop="wl_name" label="物料名称"></el-table-column>
        <el-table-column prop="wl_type" label="物料类型"></el-table-column>
        <el-table-column prop="wl_size" label="规格型号"></el-table-column>
        <el-table-column prop="kc_sl" label="库存数量"></el-table-column>
        <el-table-column label="可调拨数量">
          <template #default="{ row }">
            {{ row.kc_sl - row.kc_jhsl }}
          </template>
        </el-table-column>
      </el-table>
      <el-button @click="xgwlqx">取 消</el-button>
      <el-button type="primary" @click="wlxgqr">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ref, reactive, router, computed } from 'vue';
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus';

export default {
  setup() {
    const searchKey = ref('');
    const treeData = ref([]);
    const defaultProps = {
      children: 'children',
      label: 'label',
    };
    //获取登录角色数据
    const uid = computed(() => {
      return sessionStorage.getItem('u');
    })
    let yhid = reactive({
      id: uid
    })
    const tableData = ref([]);
    const cktableData = ref([]);
    const wltableData = ref([]);
    const selectedRows = ref([]);
    const xgselectedRows = ref([]);
    const xqtableData = ref([]);
    const wlxgtableData = ref([]);
    const selectedFilter = ref('');
    const searchTable = ref('');
    const currentPage = ref(1);
    const pageSize = ref(10);
    const totalItems = ref(0);
    let dialogVisible = ref(false);
    let xgdialogVisible = ref(false);
    let wldialogVisible = ref(false);
    let xqdialogVisible = ref(false);
    let xgwldialogVisible = ref(false);
    let form = reactive({
      wldb_code: '',
      wldb_dblx: '',
      wldb_jhdbsj: '',
      ckw_code: '',
      rkw_code: '',
      wldb_bz: '',
      // rid:'',
      uid: yhid.id
    })
    let xqform = reactive({
      wldb_code: '',
      wldb_dblx: '',
      wldb_jhdbsj: '',
      ckw_code: '',
      rkw_code: '',
      wldb_bz: '',
    })
    let zt = reactive({
      obj1: '待备货',
      obj2: '备货中',
      obj3: '已完成',
    })
    let xgform = reactive({
      wldb_id: '',
      wldb_code: '',
      wldb_dblx: '',
      wldb_jhdbsj: '',
      ckw_code: '',
      rkw_code: '',
      wldb_bz: '',
      ck_code: '',
      // rid:'',
      uid: yhid.id
    })
    function qk() {
      form.wldb_code = '',
        form.wldb_dblx = '',
        form.wldb_jhdbsj = '',
        form.ckw_code = '',
        form.rkw_code = '',
        form.wldb_bz = '',
        selectedRows.value = []
    }
    const add = () => {
      // 添加逻辑  
      dialogVisible.value = true;
    };
    async function addqr() {
      console.log(selectedRows.value)
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1并补零
      const day = String(now.getDate()).padStart(2, '0'); // 补零

      // 生成四位随机数
      const randomNumber = Math.floor(1000 + Math.random() * 9000); // 生成1000到9999之间的随机数

      // 拼接时间字符串和随机数
      const formattedTime = `${year}${month}${day}`;
      form.wldb_code = `wldb${formattedTime}${randomNumber}`;
      try {
        const response = await axios.post('/api/WLDB/Postwldb', {
          wldb_code: form.wldb_code,
          ckw_code: form.ckw_code,
          rkw_code: form.rkw_code,
          wldb_jhdbsj: form.wldb_jhdbsj,
          wldb_dblx: form.wldb_dblx,
          wldb_bz: form.wldb_bz,
          create_by: form.uid
        });
        if (response.data) {
          for (const team of selectedRows.value) {
            console.log(team)
            try {
              const membersResponse = await axios.post('/api/WLDBMX/PostWLDBMX', {
                wldb_code: form.wldb_code,
                wl_code: team.wl_code,
                wldbmx_wlmc: team.wl_name,
                wldbmx_wllx: team.wl_type,
                wldbmx_ggxh: team.wl_size,
                wldbmx_sqdbsl: team.kc_sqsl,
                kc_code: team.kc_code
              });
              if (membersResponse.data) {
                try {
                  const response = await axios.put('/api/WMS_KC/UpdateWMS_KC?id=' + team.kc_id + '&sl=' + team.kc_sqsl);
                } catch (error) {
                  console.error("请求错误:", error);
                  ElMessage.error("服务器错误，请稍后重试。");
                }
              }
            } catch (error) {
              console.error("获取班组成员数据失败:", error);
            }
          }
          ElMessage.success("成功");
          dialogVisible.value = false;
          SX();
        } else {
          ElMessage.error("添加失败");
        }
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
    }
    async function SX() {
      // 查询逻辑  
      try {
        const response = await axios.get('/api/WLDB/GetFYwldb?PageNumber=' + currentPage.value + '&PageSize=' + pageSize.value);
        tableData.value = response.data.wldbs;
        totalItems.value = response.data.zon;
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
      try {
        const response = await axios.get('/api/Wbd_ck/Wbd_Ck');
        cktableData.value = response.data;
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
    };
    SX();
    async function query() {
      //查询逻辑  
      try {
        const response = await axios.get('/api/WLDB/GetFYTJwldb?PageNumber=' + currentPage.value + '&PageSize=' + pageSize.value + '&lx=' + searchTable.value);
        tableData.value = response.data.wldbs;
        totalItems.value = response.data.zon;
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
    };
    async function editRow(row) {
      xgdialogVisible.value = true
      console.log(row);
      xgform.wldb_id = row.wldb_id,
        xgform.wldb_dblx = row.wldb_dblx,
        xgform.wldb_jhdbsj = row.wldb_jhdbsj,
        xgform.ckw_code = row.ckw_code,
        xgform.rkw_code = row.rkw_code,
        xgform.wldb_bz = row.wldb_bz
      xgform.ck_code = row.ckw_code
      xgform.wldb_code = row.wldb_code
      // 查询逻辑  
      try {
        const response = await axios.get('/api/WLDBMX/GetFYwldb?code=' + row.wldb_code);
        xqtableData.value = response.data;
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
    }
    //修改逻辑 
    async function xgqr() {
      try {
        const response = await axios.put('/api/WLDB/Updatewldb', {
          wldb_id: xgform.wldb_id,
          wldb_bz: xgform.wldb_bz,
          wldb_dblx: xgform.wldb_dblx,
          wldb_jhdbsj: xgform.wldb_jhdbsj,
          rkw_code: xgform.rkw_code,
          update_by: xgform.uid
        });
        if (response.data) {
          let sf = ref()
          let sf1 = ref()
          for (const team of xqtableData.value) {
            if (team.wldbmx_code != '') {
              const response = await axios.put('/api/WLDBMX/Updatewldbmx?code=' + team.wldbmx_code + '&sl=' + team.wldbmx_sqdbsl);
              sf.value = response.data;
              if (response.data) {
                let sl = team.wldbmx_sqdbsl - team.wldbmx_ysqsl
                const response = await axios.put('/api/WMS_KC/UpdateWMS_KC?id=' + team.kc_id + '&sl=' + sl);
              }
            } else {
              const membersResponse = await axios.post('/api/WLDBMX/PostWLDBMX', {
                wldb_code: xgform.wldb_code,
                wl_code: team.wl_code,
                wldbmx_wlmc: team.wldbmx_wlmc,
                wldbmx_wllx: team.wldbmx_wllx,
                wldbmx_ggxh: team.wldbmx_ggxh,
                wldbmx_sqdbsl: team.wldbmx_sqdbsl,
                kc_code: team.kc_code
              });
              sf1.value = membersResponse.data;
              if (membersResponse.data) {
                const response = await axios.put('/api/WMS_KC/UpdateWMS_KC?id=' + team.kc_id + '&sl=' + team.wldbmx_sqdbsl);
              }
            }

          }
          if (sf.value || sf1.value) {
            ElMessage.success("成功")
            xgdialogVisible.value = false
          }
        } else {
          ElMessage.error("失败")
        }
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
    }
    //删除逻辑
    async function deleteRow(row) {
      selectedRows.value = selectedRows.value.filter((item) => item.kc_id !== row.kc_id);
    };
    const handleCurrentChange = (page) => {
      currentPage.value = page;
      // 分页逻辑  
      SX();
    };
    async function addwl() {
      if (form.ckw_code != '') {
        dialogVisible.value = false;
        wldialogVisible.value = true;
        //查询逻辑  
        try {
          const response = await axios.get('/api/WMS_KC/GetWMS_KC?code=' + form.ckw_code);
          wltableData.value = response.data;
        } catch (error) {
          console.error("请求错误:", error);
          ElMessage.error("服务器错误，请稍后重试。");
        }
      } else {
        ElMessage.error("出库仓库未选择")
      }
    }
    function wlqx() {
      dialogVisible.value = true;
      wldialogVisible.value = false;
    }
    function wlqr() {
      dialogVisible.value = true;
      wldialogVisible.value = false;
      console.log(selectedRows.value);
    }
    async function xq(row) {
      console.log(row)
      xqdialogVisible.value = true
      xqform.wldb_dblx = row.wldb_dblx,
        xqform.wldb_jhdbsj = formatDate(row.wldb_jhdbsj),
        xqform.ckw_code = row.ck_name,
        xqform.rkw_code = row.rk_name,
        xqform.wldb_bz = row.wldb_bz
      // 查询逻辑  
      try {
        const response = await axios.get('/api/WLDBMX/GetFYwldb?code=' + row.wldb_code);
        xqtableData.value = response.data;
        console.log(xqtableData.value)
      } catch (error) {
        console.error("请求错误:", error);
        ElMessage.error("服务器错误，请稍后重试。");
      }
    }
    async function xgwl() {
      if (xgform.ck_code != '') {
        xgdialogVisible.value = false;
        xgwldialogVisible.value = true;
        //查询逻辑  
        try {
          const response = await axios.get('/api/WMS_KC/GetWMS_KC?code=' + xgform.ck_code);
          console.log(xgform.ck_code);
          wlxgtableData.value = response.data;
          for (const team of xqtableData.value) {
            const index = wlxgtableData.value.findIndex(item => item.wl_code === team.wl_code);
            if (index !== -1) {
              wlxgtableData.value.splice(index, 1);
            }
          }
        } catch (error) {
          console.error("请求错误:", error);
          ElMessage.error("服务器错误，请稍后重试。");
        }
      } else {
        ElMessage.error("出库仓库未选择")
      }
    }
    function wlxgqr() {
      xgdialogVisible.value = true;
      xgwldialogVisible.value = false;
      console.log(xqtableData.value//[0].wldb_code
      );
      for (const team of xgselectedRows.value) {
        xqtableData.value.push({
          wldbmx_id: xgselectedRows.value.length + 1,
          wl_code: team.wl_code,
          wldb_code: xqtableData.value[0].wldb_code,
          wldbmx_ggxh: team.wl_size,
          kc_sl: team.kc_sl,
          kc_jhsl: team.kc_jhsl,
          wldbmx_wllx: team.wl_type,
          wldbmx_wlmc: team.wl_name,
          wldbmx_code: '',
          kc_id: team.kc_id,
          kc_code: team.kc_code
        })
      }
      console.log(xqtableData.value);
    }
    async function deleteRow1(row) {
      if (confirm("确定要删除吗？")) {
        if (row.wldbmx_code != '') {
          const response = await axios.delete('/api/WLDBMX/Deletewldbmx?code=' + row.wldbmx_code);
          if (response.data) {
            xqtableData.value = xqtableData.value.filter((item) => item.kc_id !== row.kc_id);
            const response = await axios.put('/api/WMS_KC/UpdateWMS_KC?id=' + row.kc_id + '&sl=-' + row.wldbmx_sqdbsl);
          }
        } else {
          xqtableData.value = xqtableData.value.filter((item) => item.kc_id !== row.kc_id);
        }
      }
    }
    async function fqdb(row) {
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1并补零
      const day = String(now.getDate()).padStart(2, '0'); // 补零

      // 生成四位随机数
      const randomNumber = Math.floor(1000 + Math.random() * 9000); // 生成1000到9999之间的随机数

      // 拼接时间字符串和随机数
      const formattedTime = `${year}${month}${day}`;
      let id = `ckb${formattedTime}${randomNumber}`
      const response = await axios.post('/api/WMS_CKB/PostWMS_CKB', {
        ckb_code: id,
        ckb_type: '调拨出库',
        ck_code: row.ckw_code,
        ckb_fzr: yhid.id,
        ckb_jhcksj: row.wldb_jhdbsj,
        create_by: yhid.id,
      });
      if (response.data) {
        const response1 = await axios.get('/api/WLDBMX/GetFYwldb?code=' + row.wldb_code);
        let sf = ref("")
        for (const team of response1.data) {
          console.log(team);
          const response2 = await axios.post('/api/WMS_CKXX/PostWMS_CKXX', {
            ckb_code: id,
            wl_code: team.wl_code,
            ckxx_wlmc: team.wldbmx_wlmc,
            ckxx_wlfl: team.wldbmx_wllx,
            ckxx_ggxh: team.wldbmx_ggxh,
            ckxx_cksl: team.wldbmx_ysqsl,
            kc_code: team.kc_code,
          });
          sf.value = response2.data
        }
        if (sf.value) {
          const response3 = await axios.put('/api/WLDB/Updatewldbzt?state=2&code=' + row.wldb_code + '&ckb_code=' + id);
          if (response3.data) {
            ElMessage.success("成功")
            SX();
          }
        }
      }
    }
    function xgwlqx() {
      xgdialogVisible.value = true;
      xgwldialogVisible.value = true
    }
    const formatDate = (dateString) => {
        if (!dateString) return '';
        const date = new Date(dateString);
        return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
      };
      
    // 确保所有函数都被返回  
    return {
      searchKey,
      treeData,
      defaultProps,
      tableData,
      selectedFilter,
      searchTable,
      currentPage,
      pageSize,
      totalItems,
      add,
      query,
      editRow,
      deleteRow,
      handleCurrentChange,
      dialogVisible,
      form,
      addqr,
      xgform,
      xgdialogVisible,
      xgqr,
      addwl,
      wldialogVisible,
      wlqx,
      wlqr,
      cktableData,
      wltableData,
      selectedRows,
      xq,
      xqdialogVisible,
      xqform,
      xqtableData,
      zt,
      xgwldialogVisible,
      xgwl,
      wlxgtableData,
      wlxgqr,
      xgselectedRows,
      deleteRow1,
      fqdb,
      SX,
      xgwlqx,
      options: [{
        value: 1,
        label: '库存转移'
      }, {
        value: 2,
        label: '应急调拨'
      }, {
        value: 3,
        label: '计划性调拨'
      }],
      formatDate
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleSelectionChange(val) {
      this.selectedRows = val;
      console.log(this.selectedRows);
    },
    xghandleSelectionChange(val) {
      this.xgselectedRows = val;
      console.log(this.xgselectedRows);
    },
    handleInput(row) {
      const maxValue = row.kc_sl - row.kc_jhsl; // 计算最大值
      if (row.kc_sqsl > maxValue) {
        row.kc_sqsl = maxValue; // 如果输入值超过最大值，则修正为最大值
      }
      if (row.kc_sqsl < 0) {
        row.kc_sqsl = 0; // 如果输入值超过最大值，则修正为最大值
      }
    },
    handleInput1(row) {
      const maxValue = row.kc_sl - row.kc_jhsl; // 计算最大值
      if (row.wldbmx_sqdbsl > maxValue) {
        row.wldbmx_sqdbsl = maxValue; // 如果输入值超过最大值，则修正为最大值
      }
      if (row.wldbmx_sqdbsl < 0) {
        row.wldbmx_sqdbsl = 0; // 如果输入值超过最大值，则修正为最大值
      }
    },
  }
};
</script>

<style>
.container {
  display: flex;
  gap: 20px;
  height: 95%;
}

.sidebar {
  height: 100%;

}

.main {
  width: 80%;
  flex: 1;
  height: 100%;
}

.header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.eltable {
  height: 95%;
  font-size: 60%;
}

.letree {
  height: 100%;
}

.demo-form-inline .el-form-item {
  width: 100%;
}
</style>